package testview.zhen.com.myapplication.hencoder.practice_draw1

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

class Practice10HistogramView : View {

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //条形图
        fun drawCylinder(xl:Float,yl:Float,xr:Float,yr:Float,paint: Paint){
            canvas.drawRect(xl,yl,xr,yr,paint)
        }

        //画底部的字
        fun drawBottomText(string:String,paint: Paint,x:Float,y:Float){
            canvas.drawText(string,x,y,paint)
        }

       var h =  canvas.height
       var w =  canvas.width

        var coordinatePaint =Paint()
        coordinatePaint.style = Paint.Style.STROKE
        coordinatePaint.color = Color.WHITE
        coordinatePaint.strokeWidth = 2.0f

        //坐标轴
        var coordinateWith = w-40
        var coordinateheight = h * 3/4

        //坐标轴起点为 （20，coordinateheight）
        //纵坐标
        canvas.drawLine(20.0f,0.0f,20.0f,coordinateheight.toFloat(),coordinatePaint)
        //横坐标
        canvas.drawLine(20.0f,coordinateheight.toFloat(),(coordinateWith+20).toFloat(),coordinateheight.toFloat(),coordinatePaint)

        //柱子的绘画 柱子的空隙为25 柱子的宽为：
        var cylinderWith  = (coordinateWith - 25 * 8 )/7
        var paint = Paint()
        paint.color = Color.GREEN
        paint.style = Paint.Style.FILL
        drawCylinder(45.0f,(coordinateheight-10).toFloat(),(45.0f+cylinderWith),coordinateheight.toFloat(),paint)
        //45+cylinderWith+25
        drawCylinder((45+cylinderWith+25).toFloat(),(coordinateheight-30).toFloat(),(45+cylinderWith*2+25).toFloat(),coordinateheight.toFloat(),paint)
        drawCylinder((45+cylinderWith*2+25*2).toFloat(),(coordinateheight-30).toFloat(),(45+cylinderWith*3+25*2).toFloat(),coordinateheight.toFloat(),paint)
        drawCylinder((45+cylinderWith*3+25*3).toFloat(),(coordinateheight-220).toFloat(),(45+cylinderWith*4+25*3).toFloat(),coordinateheight.toFloat(),paint)
        drawCylinder((45+cylinderWith*4+25*4).toFloat(),(coordinateheight-400).toFloat(),(45+cylinderWith*5+25*4).toFloat(),coordinateheight.toFloat(),paint)
        drawCylinder((45+cylinderWith*5+25*5).toFloat(),(coordinateheight-440).toFloat(),(45+cylinderWith*6+25*5).toFloat(),coordinateheight.toFloat(),paint)
        drawCylinder((45+cylinderWith*6+25*6).toFloat(),(coordinateheight-160).toFloat(),(45+cylinderWith*7+25*6).toFloat(),coordinateheight.toFloat(),paint)

        //底部分字间隔为 coordinateWith/7 居中展示

        paint.style = Paint.Style.STROKE
        paint.color = Color.WHITE
        paint.textSize = 32.0f
        drawBottomText("Froyo",paint,55f,(coordinateheight+32).toFloat())
        drawBottomText("  GB  ",paint,(55+cylinderWith+25).toFloat(),(coordinateheight+32).toFloat())
        drawBottomText(" ICS ",paint,(55+cylinderWith*2+25*2).toFloat(),(coordinateheight+32).toFloat())
        drawBottomText("  JB  " ,paint,(55+cylinderWith*3+25*3).toFloat(),(coordinateheight+32).toFloat())
        drawBottomText("KitKat ",paint,(55+cylinderWith*4+25*4).toFloat(),(coordinateheight+32).toFloat())
        drawBottomText( "   L  ",paint,(55+cylinderWith*5+25*5).toFloat(),(coordinateheight+32).toFloat())
        drawBottomText( "  M   ",paint,(65+cylinderWith*6+25*6).toFloat(),(coordinateheight+32).toFloat())

        paint.textSize = 70.0f
        drawBottomText("直方图",paint,w/2-100.toFloat(),h-70.0f)

    }
}
